import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth, ApiParam } from '@nestjs/swagger';
import { Permissions, PermissionsGuard } from '@nbase/nest-mods';
import type { IRole, IPaginationResponse } from '@nbase/shared';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { QueryRoleDto } from './dto/query-role.dto';
import { AssignPermissionsDto } from './dto/assign-permissions.dto';
import { AssignUsersDto } from './dto/assign-users.dto';

/**
 * 角色控制器
 */
@ApiTags('system/role')
@ApiBearerAuth('JWT-auth')
@Controller('system/role')
@UseGuards(PermissionsGuard)
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  /**
   * 查询所有角色（分页查询）
   */
  @ApiOperation({ summary: '获取角色列表' })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:role:query')
  @Get()
  findAll(@Query() query: QueryRoleDto): Promise<IPaginationResponse<IRole>> {
    return this.roleService.paginateRoles(query);
  }

  /**
   * 根据 ID 查询角色
   */
  @ApiOperation({ summary: '获取角色详情' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Permissions('system:role:query')
  @Get(':id')
  findOne(@Param('id', ParseUUIDPipe) id: string): Promise<IRole> {
    return this.roleService.findById(id);
  }

  /**
   * 创建角色
   */
  @ApiOperation({ summary: '创建角色' })
  @ApiResponse({ status: 201, description: '创建成功' })
  @ApiResponse({ status: 400, description: '参数错误' })
  @Permissions('system:role:create')
  @Post()
  create(@Body() createRoleDto: CreateRoleDto): Promise<IRole> {
    return this.roleService.createRole(createRoleDto);
  }

  /**
   * 更新角色
   */
  @ApiOperation({ summary: '更新角色' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '更新成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Permissions('system:role:update')
  @Put(':id')
  update(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() updateRoleDto: UpdateRoleDto
  ): Promise<IRole> {
    return this.roleService.updateRole(id, updateRoleDto);
  }

  /**
   * 删除角色
   */
  @ApiOperation({ summary: '删除角色' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '删除成功' })
  @ApiResponse({ status: 404, description: '角色不存在' })
  @Permissions('system:role:delete')
  @Delete(':id')
  async remove(@Param('id', ParseUUIDPipe) id: string): Promise<void> {
    await this.roleService.deleteRole(id);
  }

  /**
   * 为角色分配权限（菜单）
   */
  @ApiOperation({ summary: '为角色分配权限' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '分配成功' })
  @Permissions('system:role:update')
  @Post(':id/permissions')
  async assignPermissions(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() assignPermissionsDto: AssignPermissionsDto
  ): Promise<void> {
    await this.roleService.assignPermissions(id, assignPermissionsDto);
  }

  /**
   * 获取角色的权限（菜单 ID 列表）
   */
  @ApiOperation({ summary: '获取角色的权限列表' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:role:query')
  @Get(':id/permissions')
  getRolePermissions(@Param('id', ParseUUIDPipe) id: string): Promise<string[]> {
    return this.roleService.getRolePermissions(id);
  }

  /**
   * 为角色分配用户
   */
  @ApiOperation({ summary: '为角色分配用户' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '分配成功' })
  @Permissions('system:role:update')
  @Post(':id/users')
  async assignUsers(
    @Param('id', ParseUUIDPipe) id: string,
    @Body() assignUsersDto: AssignUsersDto
  ): Promise<void> {
    await this.roleService.assignUsers(id, assignUsersDto);
  }

  /**
   * 获取角色的用户列表（用户 ID 列表）
   */
  @ApiOperation({ summary: '获取角色的用户列表' })
  @ApiParam({ name: 'id', description: '角色ID', type: String })
  @ApiResponse({ status: 200, description: '获取成功' })
  @Permissions('system:role:query')
  @Get(':id/users')
  getRoleUsers(@Param('id', ParseUUIDPipe) id: string): Promise<string[]> {
    return this.roleService.getRoleUsers(id);
  }
}

